package com.visitor.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.visitor.bean.SmartUser;
import com.visitor.bean.VideoFlowing;
import com.visitor.bean.Visitor;
import com.visitor.mapper.VideoFlowingMapper;
import com.visitor.mapper.VisitorMapper;
import com.visitor.utils.DateUtils;
import com.visitor.web.vm.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class VideoFlowingService {

    @Resource
    private VideoFlowingMapper videoFlowingMapper;

    public PageInfo<VideoFlowing> getAllByPage(int pageNum, int pageSize, VideoFlowing videoFlowing){
        PageHelper.startPage(pageNum, pageSize, true);
        List<VideoFlowing> list= videoFlowingMapper.selectAll(videoFlowing);
        if(list==null||list.isEmpty()) {
            list = new ArrayList<>();
        }
        return new PageInfo<>(list);
    }

    public int deleteVideoFlowingById(Long id) {
        return videoFlowingMapper.deleteByPrimaryKey(id);
    }

    public VideoFlowing findVideoFlowingById(Long id){
        VideoFlowing videoFlowing = videoFlowingMapper.selectByPrimaryKey(id);
        return videoFlowing;
    }

    @Transactional
    public Response<String> saveOrUpdate(VideoFlowing videoFlowing) {
        if(videoFlowing.getId()!=null) {
            VideoFlowing isExsit = videoFlowingMapper.isExist(videoFlowing);
            if(null==isExsit){
                return Response.error(300);
            }else{
                videoFlowingMapper.updateByPrimaryKey(videoFlowing);
                return Response.ok();
            }
        }else {
            VideoFlowing search = new VideoFlowing();
            search.setVisitorName(videoFlowing.getVisitorName());
            search.setUserName(videoFlowing.getUserName());
            search.setVisitorTime(videoFlowing.getVisitorTime());
            search.setVisitorPic(videoFlowing.getVisitorPic());
            search = videoFlowingMapper.isExist(search);
            if(null!=search){
                return Response.error(300);
            }else{
                videoFlowingMapper.insert(videoFlowing);
                return Response.ok();
            }
        }
    }
}
